﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using MC.Warehouse.Core;

namespace MC.Warehouse.Models
{
    [Serializable]
    public abstract class BaseModel : IDataErrorInfo, INotifyPropertyChanged
    {
        #region INTERFACE DATA ERROR INFO 

        public abstract string[] ValidatedProperties { get; set; }

        public abstract void Initialize();

        public bool IsValid
        {
            get
            { return ValidatedProperties.All(property => GetValidationError(property) == null); }
        }

        public abstract string GetValidationError(string propertyName);

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return GetValidationError(propertyName); }
        }

        #endregion

        #region INTERFACE NOTIFY PROPERTY CHANGED

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String propertiesName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertiesName));
            }
        }

        public void NotifyPropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyExpression.GetPropertyName()));
            }
        }

        #endregion
    }
}
